Shader "Unlit/AnimatorMeshDebugShader"
{
    Properties
    {
        _MeshDebugColor("MeshDebugColor", Color) = (1.0, 1.0, 1.0, 1.0)
    }
    SubShader
    {
        Tags
        {
            "RanderPipline" = "UniversalPipeline"
            "RanderType" = "Opaque"
        }
        LOD 100

        HLSLINCLUDE

        // 引入Core.hlsl头文件，替换UnityCG中的cginc
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

        // 将不占空间的材质相关变量放在CBUFFER中，为了兼容SRP Batcher
        CBUFFER_START(UnityPerMaterial)
           half4 _MeshDebugColor;
        CBUFFER_END

        struct Attributes
        {
            float4 vertex : POSITION;
            float4 tangent : TANGENT;
            float3 normal : NORMAL;
            float2 uv         : TEXCOORD0;
            float4 texcoord1 : TEXCOORD1;
            float4 texcoord2 : TEXCOORD2;
            float4 texcoord3 : TEXCOORD3;
            UNITY_VERTEX_INPUT_INSTANCE_ID
        };

         struct Varyings
         {
             float4 positionCS : SV_POSITION;
             UNITY_VERTEX_INPUT_INSTANCE_ID
             UNITY_VERTEX_OUTPUT_STEREO
         };

        ENDHLSL

        Pass
        {
            Name "ForwardUnlit"
            Tags {"LightMode" = "UniversalForward"}

            HLSLPROGRAM
            #pragma target 4.5
            
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.mars.gpu-instance/Shader/Include/GPUInstancing_indirect.cginc"
            #include "Packages/com.mars.gpu-instance/Shader/Include/GPUSkinning.hlsl"
            #pragma instancing_options procedural:setup
            #pragma multi_compile_instancing

            #pragma multi_compile _ DOTS_INSTANCING_ON
            #pragma multi_compile _ _USE_GLOBAL_TREE_SCALE

            half _GlobalTreeScale;

            // 声明顶点/片段着色器对应的函数
            #pragma vertex vert
            #pragma fragment frag

            // 顶点着色器
            Varyings vert(Attributes input)
            {
                Varyings output = (Varyings)0;

                UNITY_SETUP_INSTANCE_ID(input);
                UNITY_TRANSFER_INSTANCE_ID(input, output);
                gpuiCASkinning(input.texcoord2, input.texcoord3, input.vertex.xyz, input.normal.xyz, input.tangent.xyz, input.vertex.xyz, input.normal.xyz, input.tangent.xyz);          
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);

                #if defined(_USE_GLOBAL_TREE_SCALE)
                    input.vertex.xyz *= _GlobalTreeScale;
                #endif
                const VertexPositionInputs vertexInput = GetVertexPositionInputs(input.vertex.xyz);

                output.positionCS = vertexInput.positionCS;
                return output;
            }

            // 片段着色器
            half4 frag(Varyings input) : SV_Target
            {
                return _MeshDebugColor;
            }
            
            ENDHLSL
        }
    }
}
